﻿using System.Diagnostics;
using System.Text;

namespace SingleAppByMutex
{
    internal class Program
    {
        private const string MUTEX_GUID = "C1015977-21EB-4155-A54E-83E254D1C3EB";

        private static void Main(string[] args)
        {
            using (new Mutex(true, MUTEX_GUID, out bool noAppRunning))
            {
                if (noAppRunning)
                {
                    Console.WriteLine("App Run Success!\nHello, World!");
                }
                else
                {
                    var currentProcess = Process.GetCurrentProcess();
                    var otherProcess = Process.GetProcessesByName(currentProcess.ProcessName).Where(p => p.Id != currentProcess.Id).ToList();
                    string otherProcessInfo = string.Join("|", otherProcess.Select(p => $"Id={p.Id},StartTime={p.StartTime}"));
                    Console.WriteLine($"Another instance of this application({otherProcessInfo}) is already running.");
                }
                Console.ReadKey();
            }
        }
    }
}